﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMTC.WebCommon.Common;
using SMTC.BO.Plant;
using SMTC.Model.Domain;
using SMTC.Model.Model;

public partial class BaseInfoManagement_ModifyFactoryInfo : BasePage
{
    PlantProccessServices proc = new PlantProccessServices();
    PlantQueryServices query = new PlantQueryServices();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Request["action"] != null)
            {
                ViewState["action"] = Request["action"].ToString();
                if (ViewState["action"].ToString().Equals("add"))
                {
                    this.lbtitle.Text = Resources.Resource.FactoryInfoAdd;//"工厂信息添加";

                }
                else
                {
                    if (Request["PlantID"] != null)
                    {
                        ViewState["PlantID"] = Request["PlantID"].ToString();
                    }
                    this.lbtitle.Text = Resources.Resource.FactoryInfoMangement;//"工厂信息维护";

                    try
                    {
                        PlantDomain domain = query.GetlineDomain(ViewState["PlantID"].ToString());
                        this.txtPlantName.Text = domain.plant.PlantName;

                        List<SMT_BUModel> list = domain.buList;

                        foreach (SMT_BUModel item in list)
                        {
                            if (item != null)
                            {
                                ListItem li = new ListItem(item.BUName, item.BUID + "|" + item.BUCapacity);
                                this.lbAddedPU.Items.Add(li);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowMessage(Resources.Resource.ErrorForGetBUList);//"取得BU列表失败！请联系管理员"
                        return;
                    }
                    lbAddedPU.DataBind();
                }
            }
        }
    }


    protected void btnUpd_OnClick(object sender, EventArgs e)
    {
        if (!this.IsValid)
        {
            ShowMessage(Resources.Resource.ModifyFactoryInfoMsg1);//"请正确填写要输入的信息！"
            return;
        }
        if (lbAddedPU.Items.Count == 0)
        {
            ShowMessage(Resources.Resource.ModifyFactoryInfoMsg2);//"BU列表不能为空，请选择BU！"
            return;
        }
        PlantDomain domain = new PlantDomain();
        SMT_PlantModel plant = new SMT_PlantModel();
        plant.PlantName = this.txtPlantName.Text;
        plant.ModifyDateTime = DateTime.Now;
        plant.ModifyPerson = this.UserName;
        plant.PlantID = ViewState["PlantID"] == null ? Guid.NewGuid().ToString() : ViewState["PlantID"].ToString();
        domain.plant = plant;
        List<SMT_BUModel> buList = new List<SMT_BUModel>();
        foreach (ListItem item in lbAddedPU.Items)
        {
            string[] str = item.Value.Split('|');
            SMT_BUModel bu = new SMT_BUModel();
            bu.PlantID = plant.PlantID;
            bu.BUID = str[0];
            bu.BUName = item.Text;
            bu.BUCapacity = Convert.ToInt32(str[1]);
            buList.Add(bu);
        }
        domain.buList = buList;

        if (ViewState["action"].ToString().Equals("add"))
        {

            if (proc.InsertPlant(domain))
            {
                this.ShowMessage(Resources.Resource.FactoryInfoAddSuccess);//"工厂信息添加成功！"

            }
            else
            {
                this.ShowMessage(Resources.Resource.FactoryInfoAddFailed);//"工厂信息添加失败！请稍后重试！"
            }
        }
        else
        {
            if (proc.UpdatePlant(domain))
            {
                this.ShowMessage(Resources.Resource.FactoryInfoUpdSuccess);//"工厂信息维护成功！"

            }
            else
            {
                this.ShowMessage(Resources.Resource.FactoryInfoUpdFailed);//"工厂信息维护失败！请稍后重试！"
            }
        }
        this.btnUpd.Visible = false;
    }

    protected void btnAddPU_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.HidAddedPlant.Value) && this.HidAddedPlant.Value.ToLower() != "undefined")
        {
            string[] bu = this.HidAddedPlant.Value.Split(';');

            if (bu.Length == 0) return;
            for (int i = 0; i < bu.Length; i++)
            {
                bool flag = true;

                for (int j = 0; j < lbAddedPU.Items.Count; j++)
                {

                    if (lbAddedPU.Items[j].Value == bu[i].Split(',')[0].ToString() + "|" + bu[i].Split(',')[2].ToString())
                    {
                        flag = false;

                    }

                }
                if (flag)
                {
                    lbAddedPU.Items.Add(new ListItem(bu[i].Split(',')[1].ToString(), bu[i].Split(',')[0].ToString() + "|" + bu[i].Split(',')[2].ToString()));

                }

            }
        }
    
    }
    protected void btnRemovePU_Click(object sender, EventArgs e)
    {
        //if (lbAddedPU.Items.Count<=1)
        //{
        //    ShowMessage("BU列表不能为空，请保留最后一条BU数据！");
        //    return;
        //}
        while (lbAddedPU.SelectedIndex != -1)
        {
            lbAddedPU.Items.Remove(this.lbAddedPU.SelectedItem);
        }
        if (lbAddedPU.Items.Count == 0)
        {
            this.HidAddedPlant.Value = "";
        }
      

    }

 
   
}